<html>
<head><meta charset="utf-8"><title>Second opinion on API design · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Second.20opinion.20on.20API.20design.html">Second opinion on API design</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="187237712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Second%20opinion%20on%20API%20design/near/187237712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Second.20opinion.20on.20API.20design.html#187237712">(Feb 03 2020 at 06:33)</a>:</h4>
<p>I'm designing the API for a library. The library has the concept of a "tag", which the caller can provide, and which the library will give back to the caller later. The tag has to work as a filename on the filesystem, and the library receives it back from the system later as a filename and can use that to pass it back to the caller.</p>
<p>Should I:<br>
1) Accept tags as <code>AsRef&lt;OsStr&gt;</code>, and return them to the caller as <code>OsString</code>.<br>
  Advantages: no conversion, no checks, just return what I get from the OS.<br>
  Disadvantages: Pattern matching on an <code>OsString</code> feels painful.<br>
2) Accept tags as <code>&amp;str</code>, return them to the caller as <code>String</code>.<br>
  Advantages: Easy pattern matching and indexing.<br>
  Disadvantage: Conversion (<code>.to_string_lossy()</code>) every time it comes back from the OS.</p>



<a name="187238478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Second%20opinion%20on%20API%20design/near/187238478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Second.20opinion.20on.20API.20design.html#187238478">(Feb 03 2020 at 06:57)</a>:</h4>
<p>...hrm, I forgot that you also can't pattern match <code>Option&lt;String&gt;</code> using <code>Option&lt;&amp;str&gt;</code>, which makes the use of <code>String</code> not really any less painful.</p>



<a name="187240401"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Second%20opinion%20on%20API%20design/near/187240401" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dubi steinkek <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Second.20opinion.20on.20API.20design.html#187240401">(Feb 03 2020 at 07:45)</a>:</h4>
<p><code>match opt.as_deref() {}</code> should work</p>



<a name="187240710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Second%20opinion%20on%20API%20design/near/187240710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Second.20opinion.20on.20API.20design.html#187240710">(Feb 03 2020 at 07:53)</a>:</h4>
<p>Unfortunately what I have is a struct containing two fields, one of which is <code>Option&lt;String&gt;</code> (or <code>Option&lt;OsString&gt;</code>).</p>



<a name="187240768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Second%20opinion%20on%20API%20design/near/187240768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Second.20opinion.20on.20API.20design.html#187240768">(Feb 03 2020 at 07:54)</a>:</h4>
<p>So <code>as_deref()</code> by itself doesn't help.</p>



<a name="187247790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Second%20opinion%20on%20API%20design/near/187247790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Second.20opinion.20on.20API.20design.html#187247790">(Feb 03 2020 at 10:04)</a>:</h4>
<p>for pathing, you have to stick to OsString or it will block some non-utf8 windows file paths from being used. of course, maybe that's not critical for this application i guess.</p>



<a name="187278075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Second%20opinion%20on%20API%20design/near/187278075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Second.20opinion.20on.20API.20design.html#187278075">(Feb 03 2020 at 16:27)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> This library doesn't have to deal with arbitrary file paths, only paths it produced in the first place. Also, this library can't run on Windows, for unrelated reasons. (It uses UNIX sockets.)</p>



<a name="187278455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Second%20opinion%20on%20API%20design/near/187278455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Second.20opinion.20on.20API.20design.html#187278455">(Feb 03 2020 at 16:30)</a>:</h4>
<p>Then simply String / str should be sufficient.</p>
<p>Still, you'll have to give up on matching all of it at once i guess</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>